<?php 

class readModel extends appModel
{
    private $pageSize = 20;
	public function get_record(){
		$sql = "SELECT COUNT(*) FROM ".$this->table("read_vote")." WHERE vote =1 ";
		$result['first'] = $this->getOne($sql);
		$sql = "SELECT COUNT(*) FROM ".$this->table("read_vote")." WHERE vote =2 ";
		$result['second'] = $this->getOne($sql);
		return $result;
	}
	public function getMessageList($page =1,$pageSize = 20)
	{
	    $listPage=array('data'=>'','pageLink'=>'');
	    $where .= " 1=1  order by id desc ";
	    $result=$this->get_page(" id,name,phone,content ",$this->table('message'),$where,$page,$pageSize);
	    $listPage['pagelink']=$this->getpagelink();
	    $listPage['data']=$result;
	    return $listPage;
	}

	public function getApplyList($page=1)
	{
	    $where = "";
        $where .= " 1=1  order by  p1.id desc ";
        $listPage=array('data'=>'','pageLink'=>'');
        $result=$this->get_page(" p1.id,p1.name,p1.age,p1.sex,p1.phone,p1.specialty,p1.experience,p1.img_url,p2.openid,p2.nickname ",$this->table('hero_info')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid ",$where,$page,$this->pageSize);
        foreach ($result as $k=>$v)
        {
            if(strstr($v['img_url'], "[")){//有json
                $result[$k]['img_url_list'] = json_decode($v['img_url'],true);
            }else{
                $result[$k]['img_url_list'] = array($v['img_url']);
            }
        }
        $listPage['pagelink']=$this->getpagelink();
        $listPage['data']=$result;
        $sql="SELECT count(*) FROM ".$this->table('hero_info')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid WHERE ".$where;
        $count=$this->getOne($sql);
        $listPage['count']=$count;
        return $listPage;
	}
	public function getApplyAll()
	{
	    $sql = "SELECT p1.id,p1.name,p1.age,p1.sex,p1.phone,p1.specialty,p1.experience,p2.nickname,p2.openid FROM ".
	        $this->table('hero_info')." p1
					LEFT JOIN ".$this->table('wxusers')." p2
							ON p1.wxid = p2.wxid order by  id desc";
	    $result = $this->getAll($sql);
	    $i = 0;
	    foreach ($result as $k => $v)
	    {
	        $i++;
	        $result[$k]['id'] = $i;
	    }
	    return $result;
	}
	
	public function getGroupList($page=1)
	{
	    $where = "";
	    $where .= " 1=1  order by  p1.id desc ";
	    $listPage=array('data'=>'','pageLink'=>'');
	    $result=$this->get_page(" p1.id,p1.name,p1.phone,p1.reason,p2.openid,p2.nickname,img_url ",$this->table('group_info')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid ",$where,$page,$this->pageSize);
	    foreach ($result as $k=>$v)
	    {
	        if(strstr($v['img_url'], "[")){//有json
	            $result[$k]['img_url_list'] = json_decode($v['img_url'],true);
	        }else{
	            $result[$k]['img_url_list'] = array($v['img_url']);
	        }
	    }
	    $listPage['pagelink']=$this->getpagelink();
	    $listPage['data']=$result;
	    $sql="SELECT count(*) FROM ".$this->table('group_info')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid WHERE ".$where;
	    $count=$this->getOne($sql);
	    $listPage['count']=$count;
	    return $listPage;
	}
	
	public function getGroupAll()
	{
	    $sql = "SELECT p1.id,p1.name,p1.phone,p1.reason,p2.nickname,p2.openid FROM ".
	        $this->table('group_info')." p1
					LEFT JOIN ".$this->table('wxusers')." p2
							ON p1.wxid = p2.wxid order by  id desc";
	    $result = $this->getAll($sql);
	    $i = 0;
	    foreach ($result as $k => $v)
	    {
	        $i++;
	        $result[$k]['id'] = $i;
	    }
	    return $result;
	}
	
	public function getParlourList($page=1)
	{
	    $where = "";
	    $where .= " 1=1  order by  p1.id desc ";
	    $listPage=array('data'=>'','pageLink'=>'');
	    $result=$this->get_page(" p1.id,p1.name,p1.age,p1.sex,p1.tel,p1.company,p1.job,p2.openid,p2.nickname,p1.addtime ",$this->table('parlour')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid ",$where,$page,$this->pageSize);
	    foreach ($result as $k=>$v)
	    {
	    }
	    $listPage['pagelink']=$this->getpagelink();
	    $listPage['data']=$result;
	    $sql="SELECT count(*) FROM ".$this->table('parlour')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid WHERE ".$where;
	    $count=$this->getOne($sql);
	    $listPage['count']=$count;
	    return $listPage;
	}
	
	public function getParlourList2($page=1)
	{
	    $where = "";
	    $where .= " 1=1  order by  p1.id desc ";
	    $listPage=array('data'=>'','pageLink'=>'');
	    $result=$this->get_page(" p1.id,p1.name,p1.company,p1.tel,p1.job,p1.email,p1.add_time,p2.openid,p2.nickname  ",$this->table('parlour2')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid ",$where,$page,$this->pageSize);
	    foreach ($result as $k=>$v)
	    {
	    }
	    $listPage['pagelink']=$this->getpagelink();
	    $listPage['data']=$result;
	    $sql="SELECT count(*) FROM ".$this->table('parlour2')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid WHERE ".$where;
	    $count=$this->getOne($sql);
	    $listPage['count']=$count;
	    return $listPage;
	}
	
	public function getParlourAll()
	{
	    $sql = "SELECT p1.id,p1.name,p1.tel,p1.company,p1.job,p1.email,p2.nickname,p2.openid FROM ".
	        $this->table('parlour')." p1
					LEFT JOIN ".$this->table('wxusers')." p2
							ON p1.wxid = p2.wxid order by  id desc";
	    $result = $this->getAll($sql);
	    $i = 0;
	    foreach ($result as $k => $v)
	    {
	        $i++;
	        $result[$k]['id'] = $i;
	    }
	    return $result;
	}
	
	public function getParlour2All()
	{
	    $sql = "SELECT p1.id,p1.name,p1.tel,p1.company,p1.job,p1.email,p2.nickname,p2.openid FROM ".
	        $this->table('parlour2')." p1
					LEFT JOIN ".$this->table('wxusers')." p2
							ON p1.wxid = p2.wxid order by  id desc";
	    $result = $this->getAll($sql);
	    $i = 0;
	    foreach ($result as $k => $v)
	    {
	        $i++;
	        $result[$k]['id'] = $i;
	    }
	    return $result;
	}
	
	public function listComment($page = 1,$total = array())
	{
	    $where = " " ;
	    $logic = "AND";
	    foreach($total as $k=>$v)
	    {
	        if($k=="startTime"&&$v!=""){
	            $where.=" p1.create_time>=".$v." ".$logic;
	        }elseif ($k=="endTime"&&$v!=""){
	            $where.=" p1.create_time<=".$v." ".$logic;
	        }elseif($k=="comment"&&$v!=""){
	            $where.=" p1.comment like '%".$v."%' ".$logic;
	        }elseif($k=="nickname"&&$v!=""){
	            $where.=" p2.nickname like '%".$v."%' ".$logic;
	        }elseif($k=="status"){
	            if($v!="all"){
	                if($v==2)
	                {
	                    $where.=" p1.status = 0 ".$logic;
	                }else{
	                    $where.=" p1.status = '".$v."' ".$logic;
	                }
	                 
	            }
	        }
	    }
	    $where .= " 1=1  order by  p1.create_time desc ";
	    $listPage=array('data'=>'','pageLink'=>'');
	    $result=$this->get_page2(" p1.id,p1.comment,p1.status,p1.create_time,p2.openid,p2.nickname,p1.wxid ",$this->table('parlour_comment')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid  ",$where,$page,$this->pageSize);
	    if(!empty($result)){
	        foreach ($result as $k => $v)
	        {
	            if($v['wxid']== -1){
    	            $result[$k]['nickname'] = "电脑用户";
	            }
	        }
	    }
	    $listPage['pagelink']=$this->getpagelink();
	    $listPage['data']=$result;
	    return $listPage;
	}
	public function addDigest($id){
	    $result=array('error'=>0,'content'=>'精选修改成功');
	    $time = time();
	    $sql ="SELECT status FROM ".$this->table('parlour_comment')." WHERE id=$id";
	    $status = $this->getOne($sql);
	    if($status==0){
	        $sql ="UPDATE ".$this->table('parlour_comment')." SET status=1 WHERE id=$id";
	        $content = "已添加入精选";
	    }else{
	        $sql ="UPDATE ".$this->table('parlour_comment')." SET status=0 WHERE id=$id";
	        $content = "已移出精选";
	    }
	    $res=$this->query($sql);
	    if(!$res){
	        $result['error']=1;
	        $result['content']='精选修改精失败';
	    }else{
	        $result['content'] = $content;
	    }
	    return $result;
	}
	
	public function getGuangfuList($page=1)
	{
	    $where = "";
	    $where .= " 1=1  order by  p1.id desc ";
	    $listPage=array('data'=>'','pageLink'=>'');
	    $result=$this->get_page(" p1.id,p1.name,p1.phone,p1.number,p1.add_time,p2.openid,p2.nickname  ",$this->table('guangfu')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid ",$where,$page,$this->pageSize);
	    foreach ($result as $k=>$v)
	    {
	    }
	    $listPage['pagelink']=$this->getpagelink();
	    $listPage['data']=$result;
	    $sql="SELECT count(*) FROM ".$this->table('guangfu')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid WHERE ".$where;
	    $count=$this->getOne($sql);
	    $listPage['count']=$count;
	    return $listPage;
	}
	
	public function getGuangfuAll()
	{
	    $sql = "SELECT p1.id,p1.name,p1.phone,p1.number,p2.nickname,p2.openid FROM ".
	        $this->table('guangfu')." p1
					LEFT JOIN ".$this->table('wxusers')." p2
							ON p1.wxid = p2.wxid order by  id desc";
	    $result = $this->getAll($sql);
	    $i = 0;
	    foreach ($result as $k => $v)
	    {
	        $i++;
	        $result[$k]['id'] = $i;
	    }
	    return $result;
	}
	
}

